# use the official Bun image
# see all versions at https://hub.docker.com/r/oven/bun/tags
FROM oven/bun:1-alpine AS base
WORKDIR /usr/src/app

# install openssl
RUN apk add openssl

# install dependencies into temp directory
FROM base AS install
COPY bunfig.toml package.json bun.lock ./
RUN bun install --production --frozen-lockfile

# copy node_modules from temp directory
# then copy all (non-ignored) project files into the image
FROM install AS build

# install dev dependencies
RUN bun install --frozen-lockfile

# copy source code
COPY build.ts tsconfig.json ./
COPY src ./src
COPY prisma ./prisma

# generate prisma
RUN bun prisma generate

# build the app
RUN bun run build

# copy production code to final image
FROM base AS release
COPY bunfig.toml .
COPY --from=build /usr/src/app/dist .
COPY --from=build /usr/src/app/generated ./generated

# run the app
EXPOSE ${LISTEN_PORT:-3000}
ENTRYPOINT [ "bun", "index.js" ]
